package com.tools.cleanmaster.store;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.scenenew.SceneType;

/**
 * Created by wuyanfei on 15/4/17.
 * 场景化配置记录。
 */
public class SceneSharedPrefManager {
    private static final String PREFS_FILE = "pref_scene";
    //场景化通知栏消息展示时间
    private static final String PREFS_LAST_NOTIFICATION_SHOW_TIME = "last_noti_show";
    //单个场景项配置前缀，和SceneType.key结合作为key
    private static final String PREFS_SCENE_SINGLE_DATA_PREFIX = "single_scene_config_";
    //每条通知栏消息的生效时间，单位h
    private static final String PREFS_NOTIFICATION_VALID_TIME = "noti_valid_time";
    //相邻两条通知栏消息的展示时间间隔，单位h
    private static final String PREFS_NOTIFICATION_INTERVAL = "noti_interval";
    //每天可以展示的通知栏消息总数的最大值
    private static final String PREFS_NOTIFICATION_MAX_COUNT = "noti_max_count";
    //同一个场景化通知栏消息的展示时间
    private static final String PREFS_LAST_SCENE_SHOW_PREFIX = "last_scene_show_";
    //每天已经展示的通知栏消息的次数
    private static final String PREFS_NOTIFICATION_SHOW_COUNT = "noti_show_count";
    //当前正在展示的通知栏消息的类型
    private static final String PREFS_CURRENT_NOTIFICATION_TYPE = "cur_show_type";
    //下一次场景化检测重启时间
    private static final String PREFS_SCENE_TASK_RESTART_TIME = "scene_restart_time";

    // 通知栏最近一次点击时间
    private static final String PREFS_SCENE_LATEST_CLICKTIME = "prefs_scene_latest_click_time_";
    private static final String PREFS_SCENE_FIRST_IN = "prefs_scene_first_in_";    // 专清最近一次使用时间

    private static final String PREFS_SCENE_AD_NOJUDGE_SWITCH = "scene_ad_no_judge";
    private static final String PREFS_SCENE_AD_TIMEOUT = "scene_ad_timeout";


    private static SharedPreferences sPrefs = null;
    private static Context appContext;// for performance

    private static SharedPreferences initSharedPreferences(Context cxt) {
        if (sPrefs == null) {
            sPrefs = CleanApplication.getInstance().getSharedPreferences(PREFS_FILE, Activity.MODE_PRIVATE);
        }
        appContext = CleanApplication.getInstance();
        return sPrefs;
    }

    public static void setLastNotificationShowTime(long time) {
        Context appContext = CleanApplication.getInstance();
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putLong(PREFS_LAST_NOTIFICATION_SHOW_TIME, time).apply();
    }

    public static long getLastNotificationShowTime() {
        return initSharedPreferences(appContext).getLong(PREFS_LAST_NOTIFICATION_SHOW_TIME, (long) 0);
    }

    public static void setLastSceneShowTime(SceneType sceneType, long time) {
        String key = PREFS_LAST_SCENE_SHOW_PREFIX + sceneType.key;
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putLong(key, time).apply();
    }

    public static long getLastSceneShowTime(SceneType sceneType) {
        String key = PREFS_LAST_SCENE_SHOW_PREFIX + sceneType.key;
        return initSharedPreferences(appContext).getLong(key, (long) 0);
    }

    public static void setSingleSceneConfigData(SceneType sceneType, String value) {
        String key = PREFS_SCENE_SINGLE_DATA_PREFIX + sceneType.key;
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putString(key, value).apply();
    }

    public static String getSingleSceneConfigData(SceneType sceneType) {
        String key = PREFS_SCENE_SINGLE_DATA_PREFIX + sceneType.key;
        return initSharedPreferences(appContext).getString(key, "{}");
    }

    public static void setNotificationValidTime(int time) {
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putInt(PREFS_NOTIFICATION_VALID_TIME, time).apply();
    }

    public static int getNotificationValidTime() {
        return initSharedPreferences(appContext).getInt(PREFS_NOTIFICATION_VALID_TIME, 4);
    }

    public static void setNotificationInterval(int time) {
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putInt(PREFS_NOTIFICATION_INTERVAL, time).apply();
    }

    public static int getNotificationInterval() {
        return initSharedPreferences(appContext).getInt(PREFS_NOTIFICATION_INTERVAL, 3);
    }

    public static void setNotificationMaxCount(int count) {
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putInt(PREFS_NOTIFICATION_MAX_COUNT, count).apply();
    }

    public static int getNotificationMaxCount() {
        return initSharedPreferences(appContext).getInt(PREFS_NOTIFICATION_MAX_COUNT, 3);
    }

    public static void setNotificationShowCount(int count) {
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putInt(PREFS_NOTIFICATION_SHOW_COUNT, count).apply();
    }

    public static int getNotificationShowCount() {
        return initSharedPreferences(appContext).getInt(PREFS_NOTIFICATION_SHOW_COUNT, 0);
    }

    public static void setCurrentShowSceneType(SceneType type) {
        String value = type == null ? "" : type.key;
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putString(PREFS_CURRENT_NOTIFICATION_TYPE, value).apply();
    }

    /**
     * 返回当前正在展示的场景化通知栏消息的类型
     * @return may be null
     */
    public static SceneType getCurrentShowSceneType() {
        Context appContext = CleanApplication.getInstance();
        String key = initSharedPreferences(appContext).getString(PREFS_CURRENT_NOTIFICATION_TYPE, "");
        return SceneType.getTypeByKey(key);
    }

    public static long getSceneRestartTime() {
        Context appContext = CleanApplication.getInstance();
        return initSharedPreferences(appContext).getLong(PREFS_SCENE_TASK_RESTART_TIME, (long) 0);
    }

    public static void setSceneRestartTime(long time) {
        Context appContext = CleanApplication.getInstance();
        SharedPreferences prefs = initSharedPreferences(appContext);
        prefs.edit().putLong(PREFS_SCENE_TASK_RESTART_TIME, time).apply();
    }


    // 设置场景最近一次点击时间
    public static void setSceneLatestClicktime(String scene, long time){
        Context appContext = CleanApplication.getInstance();
        SharedPreferences prefs = initSharedPreferences(appContext);
        String key = PREFS_SCENE_LATEST_CLICKTIME + scene;
        prefs.edit().putLong(key, time).apply();
    }

    // 获取场景最近一次点击时间
    public static long getSceneLatestClicktime(String scene){
        Context appContext = CleanApplication.getInstance();
        String key = PREFS_SCENE_LATEST_CLICKTIME + scene;
        return initSharedPreferences(appContext).getLong(key, 0);
    }

    /**
     *是否第一次进带量页
     * @return true开屏   false锁屏
     */
    public static boolean isFirstInScene(String scene) {
        return initSharedPreferences(CleanApplication.getInstance()).getBoolean(PREFS_SCENE_FIRST_IN+scene, true);
    }

    /**
     * 设置屏幕状态
     * @param s
     */
    public static void setFirstInScene(String scene, boolean s) {
        SharedPreferences prefs = initSharedPreferences(CleanApplication.getInstance());
        prefs.edit().putBoolean(PREFS_SCENE_FIRST_IN + scene, s).apply();
    }

    public static boolean isSceneAdNoJudge(Context context) {
        return initSharedPreferences(context).getBoolean(PREFS_SCENE_AD_NOJUDGE_SWITCH, false);
    }

    public static void setSceneAdNoJudgeSwitch(Context context, boolean s) {
        SharedPreferences prefs = initSharedPreferences(context);
        prefs.edit().putBoolean(PREFS_SCENE_AD_NOJUDGE_SWITCH, s).apply();
    }

    public static int getSceneAdTimeout(Context context) {
        return initSharedPreferences(context).getInt(PREFS_SCENE_AD_TIMEOUT, 4);
    }

    public static void setSceneAdTimeout(Context context, int time) {
        SharedPreferences prefs = initSharedPreferences(context);
        prefs.edit().putInt(PREFS_SCENE_AD_TIMEOUT, time).apply();
    }
}
